package ebiz.buddhist.texts;

//import exam.andexam.C16_LongTime4.*;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Comparator;
import java.util.GregorianCalendar;

import com.nbpcorp.mobilead.sdk.MobileAdListener;
import com.nbpcorp.mobilead.sdk.MobileAdView;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TimePicker;
import android.widget.Toast;

//import ebiz.buddhist.texts.search.mpSearch;
public class AudioPlay extends Activity implements MobileAdListener {
	
	NotificationManager mNotiManager;
	ArrayList<String> mList;
	ArrayList<String> mListItem;

	ListView list;
	Cursor mCursor;
	private MobileAdView adView = null;

	int mIdx;
	int mHour, mMinute,mTmpHour=0,mTmpMinute=0;
	
	String [] tmpSaveFile;
	MediaPlayer mPlayer;
	Button mPlayBtn;
	SeekBar mProgress;
	TelephonyManager mTelMan; //------- 통화시 mp3 play 중지 
	boolean mResumeAfterCall; //------- 통화시 mp3 play 중지 
	boolean wasPlaying;
	boolean isInitCheck = false;
	private static final int SETTING_ID = Menu.FIRST; // sleep timer 
	// not used sekisuri private static final int DELETE_ID = Menu.FIRST + 1; // sleep timer
	
	static final int NAPNOTI = 1;
	Handler mHandler;// = new Handler(); 
	boolean mRunning; //= false; // sleep timer
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.playaudio);
		 // adView 만들기
	      adView = (MobileAdView) findViewById(R.id.adview1);
	      adView.setListener(this);
		  adView.start();
	    
	   
		mList = new ArrayList<String>();
		mPlayer = new MediaPlayer();
		mListItem =  searching();
		list = (ListView)findViewById(R.id.list);
		mHandler = new Handler();
		
	//	mTelMan.listen(mCallListener, PhoneStateListener.LISTEN_NONE);
	//	 NotificationManager NM = (NotificationManager) 
	//	 getSystemService(NOTIFICATION_SERVICE);
	//     NM.cancel(AudioPlay.NAPNOTI);
	     mRunning = false;
	
		ArrayAdapter<String> Adapter;
		Adapter = new ArrayAdapter<String>(this, 
				android.R.layout.simple_list_item_multiple_choice, mListItem);
		list.setAdapter(Adapter);
		list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		list.setOnItemClickListener(mItemClickListener);
		 		   
		mIdx = 0;
	
		// 버튼들의 클릭 리스너 등록
		mPlayBtn = (Button)findViewById(R.id.play);
		
		mPlayer.setOnCompletionListener(mOnComplete);
		mPlayer.setOnSeekCompleteListener(mOnSeekComplete);
		mProgress = (SeekBar)findViewById(R.id.progress);
		mProgress.setOnSeekBarChangeListener(mOnSeek);
		mProgressHandler.sendEmptyMessageDelayed(0,200);
		//------- 통화시 mp3 play 중지 
	      mTelMan = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
		  mTelMan.listen(mCallListener, PhoneStateListener.LISTEN_CALL_STATE);
		  //------- 통화시 mp3 play 중지 
		
		

	}
	 public String mpReNameHan(String str)
	 {
		   String strCompare = str;
		   if(strCompare.compareTo("chunsu.mp3")== 0)
			   strCompare = getString(R.string.item_chunsu);
		   if(strCompare.compareTo("heartsutra.mp3")== 0)
			   strCompare = getString(R.string.item_sutra);
		   if(strCompare.compareTo("beopseong.mp3")== 0)
			   strCompare = getString(R.string.item_beopseong);
		   if(strCompare.compareTo("hwaeom.mp3")== 0)
			   strCompare = getString(R.string.item_hwaeom);
		   if(strCompare.compareTo("sinju.mp3") == 0)
			   strCompare = getString(R.string.item_sinju);
//---------------------------------------------------------
		   if(strCompare.compareTo("kanmu.mp3") == 0)
			   strCompare = getString(R.string.item_kanmu);
		   if(strCompare.compareTo("bodalbomun.mp3") == 0)
			   strCompare = getString(R.string.item_bodalbomun);
		   if(strCompare.compareTo("kanja.mp3") == 0)
			   strCompare = getString(R.string.item_kanja);		  
//---------------------------------------------------------
		   
		   if(strCompare.compareTo("gwaneum.mp3") == 0)
			   strCompare = getString(R.string.item_gwaneum);
		   if(strCompare.compareTo("ami.mp3") == 0)
			   strCompare = getString(R.string.item_ami);				   
		   if(strCompare.compareTo("kukrak.mp3") == 0)
			   strCompare = getString(R.string.item_kukrak);		  
		   if(strCompare.compareTo("musang.mp3") == 0)
			   strCompare = getString(R.string.item_musang);
		   if(strCompare.compareTo("kang1.mp3") == 0)
			   strCompare = getString(R.string.item_kang1);
		   if(strCompare.compareTo("kang2.mp3") == 0)
			   strCompare = getString(R.string.item_kang2);
		   if(strCompare.compareTo("kang3.mp3") == 0)
			   strCompare = getString(R.string.item_kang3);
		   if(strCompare.compareTo("kang4.mp3") == 0)
			   strCompare = getString(R.string.item_kang4);
		   if(strCompare.compareTo("han_heartsutra.mp3") == 0)
			   strCompare = getString(R.string.item_kor_heartsutra);
		   if(strCompare.compareTo("han_chunsu.mp3") == 0)
			   strCompare = getString(R.string.item_kor_chunsu);
		   if(strCompare.compareTo("chunji.mp3") == 0)
			   strCompare = getString(R.string.item_chunji);
		   if(strCompare.compareTo("sinmyo.mp3") == 0)
			   strCompare = getString(R.string.item_sinmyo);
		   if(strCompare.compareTo("yebull.mp3")==0)
			   strCompare = getString(R.string.item_yebull);
		   if(strCompare.compareTo("prajna.mp3")==0)
			   strCompare = getString(R.string.item_prajna);
		   
		   if(strCompare.compareTo("108_7.mp3") == 0)
			   strCompare = getString(R.string.item_bows07);
		   if(strCompare.compareTo("108_8.mp3") == 0)
			   strCompare = getString(R.string.item_bows08);
		   if(strCompare.compareTo("108_9.mp3") == 0)
			   strCompare = getString(R.string.item_bows09);
		   if(strCompare.compareTo("108_10.mp3") == 0)
			   strCompare = getString(R.string.item_bows10);
		   if(strCompare.compareTo("108_11.mp3") == 0)
			   strCompare = getString(R.string.item_bows11);
		   if(strCompare.compareTo("108bow.mp3") == 0)
			   strCompare = getString(R.string.item_bows_s);
		   if(strCompare.compareTo("108bow_youngin.mp3") == 0)
			   strCompare = getString(R.string.item_bows_youngin);
		   
		   if(strCompare.compareTo("MedicineBuddha.mp3") == 0)
			   strCompare = getString(R.string.item_medicine_buddha);

		   if(strCompare.compareTo("MantraOfBodhisattvaCundi.mp3") == 0)
			   strCompare = getString(R.string.cundi);

		   if(strCompare.compareTo("Monks_of_Tibet_Healing.mp3") == 0)
			   strCompare = getString(R.string.monks);

		   if(strCompare.compareTo("Korean_Zen_master.mp3") == 0)
			   strCompare = getString(R.string.zen_master);

		   
		   if(strCompare.compareTo("chant.mp3") == 0)
			   strCompare = getString(R.string.item_chant);

		   if(strCompare.compareTo("followyourheart.mp3") == 0)
			   strCompare = getString(R.string.item_followyourheart);

		   if(strCompare.compareTo("windy_hermitage.mp3") == 0)
			   strCompare = getString(R.string.item_windy_hermitage);

		   if(strCompare.compareTo("healing_music.mp3") == 0)
			   strCompare = getString(R.string.item_healing_music);
		   
		   if(strCompare.compareTo("Buddhist_hymn3.mp3") == 0)
			   strCompare = getString(R.string.Buddhist_hymn_3);

	
		   if(strCompare.compareTo("diamond_sutra.mp3") == 0)
			   strCompare = getString(R.string.item_diamond) ;
		   
		   if(strCompare.compareTo("sakyamuni.mp3") == 0)
			   strCompare = getString(R.string.item_sakyamuni);
		 
		   if(strCompare.compareTo("guan.mp3") == 0)
			   strCompare = getString(R.string.item_guan);
		   
		   
		   if(strCompare.compareTo("jeongmok_1.mp3") == 0)
			   strCompare = getString(R.string.mp3_meditation);
		   
		   if(strCompare.compareTo("jeongmok_2.mp3") == 0)
			   strCompare = getString(R.string.mp3_meditation_2);
		   
		   if(strCompare.compareTo("semin_hwaeom.mp3") == 0)
			   strCompare = getString(R.string.item_semin2);
		   if(strCompare.compareTo("semin_valwon.mp3") == 0)
			   strCompare = getString(R.string.mp_valwon);
		   
		   return strCompare;
	 }
	 public String mpReNameEn(String str)
	 {
		 String strCompare = str;
		   if(strCompare.compareTo(getString(R.string.item_chunsu))== 0)
			   strCompare = "chunsu.mp3";
		   if(strCompare.compareTo(getString(R.string.item_sutra))== 0)
			   strCompare = "heartsutra.mp3";
		   if(strCompare.compareTo(getString(R.string.item_beopseong))== 0)
			   strCompare = "beopseong.mp3";
		   if(strCompare.compareTo(getString(R.string.item_hwaeom))== 0)
			   strCompare = "hwaeom.mp3";
		   if(strCompare.compareTo(getString(R.string.item_sinju)) == 0)
			   strCompare = "sinju.mp3";

 //---------------  음질이 안좋아서 삭제
		   if(strCompare.compareTo(getString(R.string.item_kanmu)) == 0)
			   strCompare = "kanmu.mp3";
		   if(strCompare.compareTo(getString(R.string.item_bodalbomun)) == 0)
			   strCompare = "bodalbomun.mp3";
		   if(strCompare.compareTo(getString(R.string.item_kanja)) == 0)
			   strCompare = "kanja.mp3";	
//-----------------------------------
		   
		   if(strCompare.compareTo(getString(R.string.item_gwaneum)) == 0)
			   strCompare = "gwaneum.mp3";
		   if(strCompare.compareTo(getString(R.string.item_ami)) == 0)
			   strCompare = "ami.mp3";
		   
		   if(strCompare.compareTo(getString(R.string.item_kukrak)) == 0)
			   strCompare = "kukrak.mp3";
		   if(strCompare.compareTo(getString(R.string.item_musang)) == 0)
			   strCompare = "musang.mp3";
		   if(strCompare.compareTo(getString(R.string.item_kang1)) == 0)
			   strCompare = "kang1.mp3";
		   if(strCompare.compareTo(getString(R.string.item_kang2)) == 0)
			   strCompare = "kang2.mp3";
		   if(strCompare.compareTo(getString(R.string.item_kang3)) == 0)
			   strCompare = "kang3.mp3";
		   if(strCompare.compareTo(getString(R.string.item_kang4)) == 0)
			   strCompare = "kang4.mp3";	   
		   if(strCompare.compareTo(getString(R.string.item_kor_heartsutra)) == 0)
			   strCompare = "han_heartsutra.mp3";
		   if(strCompare.compareTo(getString(R.string.item_kor_chunsu)) == 0)
			   strCompare = "han_chunsu.mp3";
		   if(strCompare.compareTo(getString(R.string.item_chunji)) == 0)
			   strCompare = "chunji.mp3";
		   if(strCompare.compareTo(getString(R.string.item_sinmyo)) == 0)
			   strCompare = "sinmyo.mp3";
		   if(strCompare.compareTo(getString(R.string.item_yebull))==0)
			   strCompare = "yebull.mp3";
		   if(strCompare.compareTo(getString(R.string.item_prajna))==0)
			   strCompare = "prajna.mp3";
		   
		   if(strCompare.compareTo(getString(R.string.item_bows07)) == 0)
			   strCompare = "108_7.mp3";
		   if(strCompare.compareTo(getString(R.string.item_bows08)) == 0)
			   strCompare = "108_8.mp3";
		   if(strCompare.compareTo(getString(R.string.item_bows09)) == 0)
			   strCompare = "108_9.mp3";
		   if(strCompare.compareTo(getString(R.string.item_bows10)) == 0)
			   strCompare = "108_10.mp3";
		   if(strCompare.compareTo(getString(R.string.item_bows11)) == 0)
			   strCompare = "108_11.mp3";
		   if(strCompare.compareTo(getString(R.string.item_bows_s)) == 0)
			   strCompare = "108bow.mp3";
		   if(strCompare.compareTo(getString(R.string.item_bows_youngin)) == 0)
			   strCompare = "108bow_youngin.mp3";
		 
		   if(strCompare.compareTo(getString(R.string.item_medicine_buddha)) == 0)
			   strCompare = "MedicineBuddha.mp3";
		   if(strCompare.compareTo(getString(R.string.cundi)) == 0)
			   strCompare = "MantraOfBodhisattvaCundi.mp3";
		   if(strCompare.compareTo(getString(R.string.monks)) == 0)
			   strCompare = "Monks_of_Tibet_Healing.mp3";
		   if(strCompare.compareTo(getString(R.string.zen_master)) == 0)
			   strCompare = "Korean_Zen_master.mp3";
		   
		   if(strCompare.compareTo(getString(R.string.item_chant)) == 0)
			   strCompare = "chant.mp3";
		   if(strCompare.compareTo(getString(R.string.item_followyourheart)) == 0)
			   strCompare = "followyourheart.mp3";
		   if(strCompare.compareTo(getString(R.string.item_windy_hermitage)) == 0)
			   strCompare = "windy_hermitage.mp3";
		   if(strCompare.compareTo(getString(R.string.item_healing_music)) == 0)
			   strCompare = "healing_music.mp3";
		   
		   if(strCompare.compareTo(getString(R.string.Buddhist_hymn_3)) == 0)
			   strCompare = "Buddhist_hymn3.mp3";
		   

		   if(strCompare.compareTo(getString(R.string.item_diamond)) == 0)
			   strCompare = "diamond_sutra.mp3";
		   
		   if(strCompare.compareTo(getString(R.string.item_sakyamuni)) == 0)
			   strCompare = "sakyamuni.mp3";
		   
		   if(strCompare.compareTo(getString(R.string.item_guan)) == 0)
			   strCompare = "guan.mp3";
		  
		
		   if(strCompare.compareTo(getString(R.string.mp3_meditation)) == 0)
			   strCompare = "jeongmok_1.mp3";
		   if(strCompare.compareTo(getString(R.string.mp3_meditation_2)) == 0)
			   strCompare = "jeongmok_2.mp3";
		   
		   if(strCompare.compareTo(getString(R.string.item_semin2)) == 0)
			   strCompare = "semin_hwaeom.mp3";
		   if(strCompare.compareTo(getString(R.string.mp_valwon)) == 0)
			   strCompare = "semin_valwon.mp3";
		   
		   return strCompare;
		   
	 }
	 public String FileNameSearch(String strPath)
		{
		    String path;
			File SDCardRoot = Environment.getExternalStorageDirectory(); 
	        File dir = new File(SDCardRoot + "/buddhist/");
			if(dir!=null) {
				File[] dirs = dir.listFiles();
				if(dirs!=null) {
					for(int i=0; i<dirs.length; i++) {
						
						if(dirs[i].getName().endsWith("mp3")) {
							path = dirs[i].getName();
							if(path.compareTo(strPath) == 0) 
								return dirs[i].getAbsolutePath();
						}
					}
				}
			}
			
			return  strPath;
		}
	public  ArrayList<String> searching() {
		ArrayList<String> searchList = new ArrayList<String>();		
		String strCompare=null;
		ArrayList<String> sortList = new ArrayList<String>();
		File SDCardRoot = Environment.getExternalStorageDirectory(); 
        File dir = new File(SDCardRoot + "/buddhist/");
		if(dir!=null) {
			File[] dirs = dir.listFiles();
			if(dirs!=null) {
				for(int i=0; i<dirs.length; i++) {
					if(dirs[i].getName().endsWith("mp3")) {
					//	searchList.add(dirs[i].getAbsolutePath());		
					//	sortStr[i] = dirs[i].getName();
						strCompare = mpReNameHan(dirs[i].getName());
						sortList.add(strCompare);
					}
				}
			}
		}
		// 리스트를 배열로 변환
	     Object[] obj = sortList.toArray();
	     // API에서 Arrays.sort 참조 ( 정렬의 실체임 )
	     Arrays.sort(obj, new Comparator<Object>() {
	          public int compare(Object o1, Object o2) {
	               String t1 = (String) o1;
	               String t2 = (String) o2;
	               return t1.compareTo(t2);
	          }
	     });
	          
	     for (int i = 0; i < obj.length; i++) {
	    	 searchList.add((String) obj[i]);
	    	 }
		
		return  searchList;
	}
	  public void mOnClick(View v) {
						
		    if(isInitCheck == false || mList.isEmpty()== true)
		    {
		    	if(mPlayer.isPlaying()== true)
		    	{
		    		mPlayer.reset();
		    		mPlayBtn.setText(getString(R.string.btn_play));
		    		mProgress.setProgress(0);
		    	}
		    	Toast.makeText(AudioPlay.this, getString(R.string.message_marking), 1).show();
		    	return;
		    }
	    	switch (v.getId()) {
	    	
	        // 재생 및 일시 정지
	    	case R.id.play:
				if (mPlayer.isPlaying() == false) {
					mPlayer.start();
					mPlayBtn.setText(getString(R.string.btn_pause));
				} else {
					mPlayer.pause();
					mPlayBtn.setText(getString(R.string.btn_play));
				}
	   
	    		break;
	   		// 재생 정지. 재시작을 위해 미리 준비해 놓는다.
	    	case R.id.stop:
				mPlayer.stop();
				mPlayBtn.setText(getString(R.string.btn_play));
				mProgress.setProgress(0);
				Prepare();
	    		break;
	    	case R.id.prev:
	    	case R.id.next:
				boolean wasPlaying = mPlayer.isPlaying();
				
				if (v.getId() == R.id.prev) {
					mIdx = (mIdx == 0 ? mList.size() - 1:mIdx - 1);
				} else {
					mIdx = (mIdx == mList.size() - 1 ? 0:mIdx + 1);
				}
				
				mPlayer.reset();
				LoadMedia(mIdx);

				// 이전에 재생중이었으면 다음 곡 바로 재생
				if (wasPlaying) {
					mPlayer.start();
					mPlayBtn.setText(getString(R.string.btn_pause));
				}
	    		break;
	    	}
	    }

	
  // 항상 준비 상태여야 한다.
  boolean LoadMedia(int idx) {
		try {
			/*if(isAddDelCheck == true)
			{
				if(mList.isEmpty() == true)
				{
				 mPlayer.release();
				 mPlayer = null;
				}
				
				
			}*/
			//else
		//	{
			mPlayer.setDataSource(mList.get(idx));
		//	}
		} catch (IllegalArgumentException e) {
			return false;
		} catch (IllegalStateException e) {
			return false;
		} catch (IOException e) {
			return false;
		}
		if (Prepare() == false) {
			return false;
		}
		mProgress.setMax(mPlayer.getDuration());
		
		return true;
  }
  
  boolean Prepare() {
		try {
			mPlayer.prepare();
		} catch (IllegalStateException e) {
			return false;
		} catch (IOException e) {
			return false;
		}
		return true;
  }
 
// 재생 완료되면 다음곡으로
  MediaPlayer.OnCompletionListener mOnComplete = 
  	new MediaPlayer.OnCompletionListener() {
		public void onCompletion(MediaPlayer arg0) {
			mIdx = (mIdx == mList.size() - 1 ? 0:mIdx + 1);
			mPlayer.reset();
			LoadMedia(mIdx);
			mPlayer.start();
		}
  };

  // 에러 발생시 메시지 출력
  MediaPlayer.OnErrorListener mOnError = 
  	new MediaPlayer.OnErrorListener() {
		public boolean onError(MediaPlayer mp, int what, int extra) {
			String err = "OnError occured. what = " + what + " ,extra = " + extra;
			Toast.makeText(AudioPlay.this, err, Toast.LENGTH_LONG).show();
			return false;
		}
  };

  // 위치 이동 완료 처리
  MediaPlayer.OnSeekCompleteListener mOnSeekComplete = 
  	new MediaPlayer.OnSeekCompleteListener() {
		public void onSeekComplete(MediaPlayer mp) {
			if (wasPlaying) {
				mPlayer.start();
			}
		}
  };

  // 0.2초에 한번꼴로 재생 위치 갱신
	Handler mProgressHandler = new Handler() {
		public void handleMessage(Message msg) {
			if (mPlayer == null) return;
			if (mPlayer.isPlaying()) {
				mProgress.setProgress(mPlayer.getCurrentPosition());
			}
			mProgressHandler.sendEmptyMessageDelayed(0,200);
		}
	};

	// 재생 위치 이동
	SeekBar.OnSeekBarChangeListener mOnSeek = 
		new SeekBar.OnSeekBarChangeListener() {
		public void onProgressChanged(SeekBar seekBar, 
				int progress, boolean fromUser) {
			if (fromUser) {
				mPlayer.seekTo(progress);
			}
		}

		public void onStartTrackingTouch(SeekBar seekBar) {
			wasPlaying = mPlayer.isPlaying();
			if (wasPlaying) {
				mPlayer.pause();
			}
		}

		public void onStopTrackingTouch(SeekBar seekBar) {
		}
	};
	
	  //------- 통화시 mp3 play 중지 
	PhoneStateListener mCallListener = new PhoneStateListener() {
		public void onCallStateChanged (int state, String incomingNumber) {
			switch (state) {
			case TelephonyManager.CALL_STATE_IDLE:
				if (mResumeAfterCall) {
				/*	
					mTextCall.postDelayed(new Runnable() {
						public void run() {
							mPlayer.start();
						}
					}, 3000);
				*/	
					mPlayer.start();
				}
				break;
			case TelephonyManager.CALL_STATE_RINGING:
				// 전화오면 즉시 재생 중지
				if (mPlayer.isPlaying()) {
					mPlayer.pause();
					mResumeAfterCall = true;
				} else {
					mResumeAfterCall = false;
				}
				break;
			case TelephonyManager.CALL_STATE_OFFHOOK:
				break;
			}
		}
	};
	
	  //------- 통화시 mp3 play 중지 
	  @Override
	    public boolean onCreateOptionsMenu(Menu menu) {
	    	super.onCreateOptionsMenu(menu);
	    	menu.add(0, SETTING_ID, 0, getString(R.string.menu_timer));
	    	return true;
	    }
	    
	    @Override
	    public boolean onMenuItemSelected(int featureId, MenuItem item) {
	    	super.onMenuItemSelected(featureId, item);
	    	
	    	switch(item.getItemId()) {
	    	case SETTING_ID:
	    
	    		Calendar cal = new GregorianCalendar();
	    		mHour = cal.get(Calendar.HOUR_OF_DAY);
	    		mMinute = cal.get(Calendar.MINUTE);
	    		TimePickerDialog timePickerDialog = new TimePickerDialog(this, new OnTimeSetListener() {
					public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
						 if(hourOfDay < mHour)
						 {
							 mTmpHour = 24+hourOfDay - mHour;
						 }
						 else
						 {
							 mTmpHour = hourOfDay - mHour;
						 }
						 if(minute < mMinute)
						 {
							 mTmpMinute = 60+minute - mMinute;
						 }
						 else
						 {
							 mTmpMinute = minute - mMinute;
						 }
						 if(mTmpHour <= 0 && mTmpMinute <= 0)
						 {
							Toast.makeText(AudioPlay.this, getString(R.string.message_timer),Toast.LENGTH_SHORT).show();
						 }
						 else
						 {
							 mNotiManager = (NotificationManager)getSystemService(
						        		NOTIFICATION_SERVICE);	
							 mRunning = true;
							 mHandler.postDelayed(new Runnable() {
									public void run() {
						    		
						    			finish();
									}
								}, ((mTmpHour*3600)+(mTmpMinute*60)) * 1000);
													 
							 @SuppressWarnings("deprecation")
							Notification noti = new Notification(R.drawable.main_icon,
									 mTmpHour + getString(R.string.notification_hour) + 
									 mTmpMinute+ getString(R.string.notification_min) + 
									 getString(R.string.notification_aftertime),System.currentTimeMillis());
							 Intent intent = new Intent(AudioPlay.this, setSleepTimer.class);
				    		 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				    			
							 PendingIntent content = PendingIntent.getActivity(
				    					AudioPlay.this, 0, intent, 0);
				    		 noti.setLatestEventInfo(AudioPlay.this, getString(R.string.notification_finaltime), 
				    				 hourOfDay + getString(R.string.notification_hour)
				    				 + minute + getString(R.string.notification_min), content);
				    		mNotiManager.notify(AudioPlay.NAPNOTI, noti);
				    		
						 }

					}
				}, mHour, mMinute, true);
				timePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
					public void onCancel(DialogInterface dialog) {
						Toast.makeText(AudioPlay.this, "onCancel",Toast.LENGTH_SHORT).show();

					}
				});
				timePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE
						, "Cancel"
						, new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
							}
						});
				timePickerDialog.show();
				break;
	    
	    	}
	    	
	    	return true;
	    }
	    
	AdapterView.OnItemClickListener mItemClickListener = 
		new AdapterView.OnItemClickListener() {
		public void onItemClick(AdapterView parent, View view, int position, long id) {
			String path;
			String strChainge = mpReNameEn(mListItem.get(position));
			String strCompareName;
			String strTmp;
			boolean isAddDelCheck = false;
			File SDCardRoot = Environment.getExternalStorageDirectory(); 
			path = SDCardRoot + "/buddhist/";
			strTmp = path+strChainge;
			if(mList.isEmpty() == true)
			{
				mList.add(path+strChainge);
				isInitCheck = true;
			}
			else
			{
				for(int i=0; i <mList.size();i++)
				{
					strCompareName = mList.get(i);
					if(strCompareName.compareTo(strTmp)==0)
					{
						ArrayList<String> tmpList = new ArrayList<String>();
						tmpList.add(strTmp);
						mList.removeAll(tmpList);
						isAddDelCheck = true;
					}
				}
				
			/*	if(mList.isEmpty()== true)
				{
					if(mPlayer.isPlaying()== true)
			    	{
			    		mPlayer.reset();
			    		mPlayBtn.setText("Play");
			    		mProgress.setProgress(0);
			    	}
					return;
				}*/
				if(isAddDelCheck == false)
				{
					mList.add(path+strChainge);
					isInitCheck = true;
				}
			}
			if(mList.isEmpty() == true)	
				isInitCheck = false;
			
			if(mPlayer.isPlaying() == false && mList.size()== 1)
			{
				
				if (LoadMedia(mIdx) == false) {
					
				//	finish();
		   		}
			}
		}
	};

	@Override
	protected void onDestroy() {
		super.onDestroy();
		
		if (adView != null) {
			adView.destroy();
			adView = null;
		}
		 if(mRunning == true)
	      {
	    	  NotificationManager NM = (NotificationManager) 
	  		  getSystemService(NOTIFICATION_SERVICE);
	          NM.cancel(AudioPlay.NAPNOTI);
	      }
	      if (mPlayer != null) {
	      	mPlayer.release();
	      	mPlayer = null;
	      }
			mTelMan.listen(mCallListener, PhoneStateListener.LISTEN_NONE);		
	}

	public void onReceive(int err) {
		// event for receive ad 
		
	}
}
